package com.zealcomm.ivcscustomer.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.wang.avi.AVLoadingIndicatorView;

import java.util.HashMap;
import java.util.Map;

public class TipsService extends Service {


    private WindowManager windowManager;
    private WindowManager.LayoutParams mlayoutParams;
    private Map<String,AVLoadingIndicatorView> mAVLoadingMap = new HashMap<String,AVLoadingIndicatorView>();
    private int mStatusH;
    private int mDiameter = 100;
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(mDiameter,mDiameter);

    @Override
    public IBinder onBind(Intent intent) {
        return new TipsBinder();
    }

    public class TipsBinder extends Binder {

        public void callShowOver(String id, int x, int y){
            showOver(id, x, y);
        }

        public void callHideOver(String id){
            hideOver(id);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mlayoutParams = new WindowManager.LayoutParams();
        int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
        mStatusH = this.getResources().getDimensionPixelSize(resourceId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void showOver(String id, int x, int y) {
        AVLoadingIndicatorView iAVLoadingIndicatorView = new AVLoadingIndicatorView(this);
        iAVLoadingIndicatorView.setLayoutParams(layoutParams);
        iAVLoadingIndicatorView.setIndicatorColor(Color.RED);
        iAVLoadingIndicatorView.setIndicator("BallScaleIndicator");
        iAVLoadingIndicatorView.bringToFront();
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

        //8.0系统加强后台管理，禁止在其他应用和窗口弹提醒弹窗，如果要弹，必须使用 TYPE_APPLICATION_OVERLAY
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mlayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else {
            mlayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }

        mlayoutParams.width = mDiameter;
        mlayoutParams.height = mDiameter;
        iAVLoadingIndicatorView.show();
        mlayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        mlayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mlayoutParams.format = PixelFormat.RGBA_8888 | PixelFormat.TRANSLUCENT;
        mlayoutParams.x = x - mlayoutParams.width/2;
        mlayoutParams.y = y - mlayoutParams.height/2 - mStatusH;

        windowManager.addView(iAVLoadingIndicatorView, mlayoutParams);
        mAVLoadingMap.put(id, iAVLoadingIndicatorView);
    }

    private void hideOver(String id) {
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        windowManager.removeViewImmediate(mAVLoadingMap.get(id));
        mAVLoadingMap.remove(id);
    }

}